package org.lq.ht.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.lq.ht.dao.RoomDao;
import org.lq.ht.entity.Room;
import org.lq.ht.util.Instantiation;
import org.lq.ht.util.JDBCUtil;
/**
 * 房间数据访问层接口实现
 *@author 张冲
 * @qq2865346830
 * @package org.lq.ht.dao.impl
 * @date 2020年8月21日下午8:04:01
 */
public class RoomDaoImpl  implements RoomDao,Instantiation<Room>{

	@Override
	public int save(Room t) {
		String sql ="insert into room (room_number,type,room_price,status,note) values(?,?,?,?,?)";
		int num =0;
		try {
			num = JDBCUtil.executeUpdate(sql, t.getRoom_number(),t.getType(),t.getRoom_price(),t.getStatus(),t.getNote());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return num;
	}

	@Override
	public int update(Room t) {
		String sql ="update room set room_number=?,type=?,room_price=?,status=?,note=? where no =?";
		int num =0;
		try {
			num =JDBCUtil.executeUpdate(sql, t.getRoom_number(),t.getType(),t.getRoom_price(),t.getStatus(),t.getNote(),t.getNo());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return num;
	}

	@Override
	public int delete(int id) {
		String sql ="delete from room where no=?";
		int num =0;
		try {
			num=JDBCUtil.executeUpdate(sql, id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return num;
	}

	@Override
	public List<Room> findAll() {
		List<Room> list = new ArrayList<Room>();
		String sql ="select * from room";
		try {
			list=JDBCUtil.executeQuery(sql, this);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public Room getById(int id) {
		List<Room> list = new ArrayList<Room>();
		String sql ="select * from room where no =?";
		try {
			list=JDBCUtil.executeQuery(sql, this,id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list.get(0);
	}

	@Override
	public Room instance(ResultSet rs) {
		Room r = new Room();
		try {
			r.setNo(rs.getInt("no"));
			r.setRoom_number(rs.getInt("room_number"));
			r.setType(rs.getString("type"));
			r.setRoom_price(rs.getString("room_price"));
			r.setStatus(rs.getString("status"));
			r.setNote(rs.getString("note"));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return r;
	}
	



}


